﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Pub
{
    public class Cpho
    {
        CADO ado1 = new CADO();
        AdoMSSQL adozcd = new AdoMSSQL("DBCon_ZCD");

        #region 计算一段时某一站点降水距平
        public void CaluRainJp(string staid, string bDate, string eDate, ref double RainJp)
        {
            DataRow f_dr1;
            DataSet f_ds1;
            string f_sql = "";
            double f_rain = 0, f_rain1 = 0;
            f_sql = "select SUM( Precipitation) as Rain  from HIS_DAY_" + eDate.Substring(0, 4) + "  where StationNum='" + staid + "' "
                + "  and ObservTimes>'" + bDate + "20' and ObservTimes<='" + eDate + "20' and ObservTimes like '%20'";
            f_ds1 = adozcd.sel1(f_sql);
            if (!CDB.dsIsNull(f_ds1))
            {
                f_dr1 = f_ds1.Tables[0].Rows[0];
                if (f_dr1["rain"].ToString() != "")
                    f_rain = Convert.ToDouble(f_dr1["rain"].ToString());
                else
                    f_rain = 0;
                f_sql = " select SUM(r) as rain  from lyrzl where station = '" + staid + "'  and date1>= '" + Convert.ToInt32(bDate.Substring(4, 2)) + "' and date1<='" + Convert.ToInt32(eDate.Substring(4, 2)) + "' ";
                f_ds1 = adozcd.sel1(f_sql);
                f_dr1 = f_ds1.Tables[0].Rows[0];
                if (f_dr1["rain"].ToString() != "")
                    f_rain1 = Convert.ToDouble(f_dr1["rain"].ToString());
                else
                    f_rain1 = 0;
                RainJp = f_rain - f_rain1;
            }
            else
            {
                RainJp = 0;
            }
        }

        #endregion

        #region 计算一段时间某一站点温度距平
        public void CaluTempJp(string staid, string bDate, string eDate, ref double TmpJp)
        {
            DataRow f_dr1;
            DataSet f_ds1;
            string f_sql = "";
            double f_temp = 0, f_temp1 = 0;
            f_sql = "select avg( DryBulTemp) as DryBulTemp  from HIS_DAY_" + eDate.Substring(0, 4) + "  where StationNum='" + staid + "' "
                + "  and ObservTimes>'" + bDate + "20' and ObservTimes<='" + eDate + "20' and ObservTimes like '%20'";
            f_ds1 = adozcd.sel1(f_sql);
            if (!CDB.dsIsNull(f_ds1))
            {
                f_dr1 = f_ds1.Tables[0].Rows[0];
                if (f_dr1["DryBulTemp"].ToString() != "")
                    f_temp = Convert.ToDouble(f_dr1["DryBulTemp"].ToString());
                else
                    f_temp = 0;
                f_sql = " select avg(tm) as DryBulTemp  from lyrzl where station = '" + staid + "'  and date1>= '" + Convert.ToInt32(bDate.Substring(4, 2)) + "' and date1<='" + Convert.ToInt32(eDate.Substring(4, 2)) + "' ";
                f_ds1 = adozcd.sel1(f_sql);
                f_dr1 = f_ds1.Tables[0].Rows[0];
                if (f_dr1["DryBulTemp"].ToString() != "")
                    f_temp1 = Convert.ToDouble(f_dr1["DryBulTemp"].ToString());
                else
                    f_temp1 = 0;
                TmpJp = f_temp - f_temp1;
            }
            else
            {
                TmpJp = 0;
            }
        }

        #endregion

        #region 提取精细化报文
        public void CaluRffc(string staid, string RffcDate, string HH, ref string phenomena, ref string maxtemp, ref string mintemp, ref string windirect, ref string windspeed)
        {
            DataRow f_dr1;
            DataSet f_ds1;
            string f_strsql = " select   phenomena,windirect,windspeed,maxtemp,mintemp  from rffc_tab_element where    stationnum = '" + staid + "' "
                            + "  and ObservTm  = '" + RffcDate + "1630'  and time_hh='" + HH + "'  ";
            f_ds1 = ado1.sel1(f_strsql);

            if (!CDB.dsIsNull(f_ds1))
            {
                f_dr1 = f_ds1.Tables[0].Rows[0];
                phenomena = f_dr1["phenomena"].ToString();
                windirect = f_dr1["windirect"].ToString();
                windspeed = f_dr1["windspeed"].ToString();
                maxtemp = f_dr1["maxtemp"].ToString();
                mintemp = f_dr1["mintemp"].ToString();
            }
        }

        #endregion


        #region 判断连阴雨最长天数
        public void IsDis(string bDate, string eDate, string staid, ref int RainCount, ref Double AvgTemp, ref Double MaxTemp, ref Double MinTemp, ref int WindV, ref string Wind)
        {
            DataRow f_dr2;
            int iCount = 0;//计数器
            int MaxCount = 0;//最大计数器
            int PreV = 0;
            String IsOk = "no";


            //一、连阴雨查询
            //string f_strsql = " select  phenomena  from rffc_tab_element where    stationnum = '" + staid + "' "
            //                + " and ObservTm >= '" + bDate + "0000' "
            //                + " and ObservTm <= '" + eDate + "2300'  and  ObservTm like '%1630'  and time_hh=24 and  ( phenomena like '%雨%'  or  phenomena like '%雪%'  )  ";
            string f_strsql = " select  phenomena  from rffc_tab_element where    stationnum = '" + staid + "' "
                            + "  and ObservTm  = '" + eDate + "1630'  and     ( phenomena like '%雨%'  or  phenomena like '%雪%'  )  ";


            DataSet f_ds2 = ado1.sel1(f_strsql);
            for (int i = 0; i < f_ds2.Tables[0].Rows.Count; i++)
            {
                f_dr2 = f_ds2.Tables[0].Rows[i];
                //首先判断当前天气现象是否有雨
                if (f_dr2["phenomena"].ToString().IndexOf("雨") != -1)
                {
                    IsOk = "yes";
                }
                else
                {
                    IsOk = "no";
                }
                //如果有雨,判断是不是和前一天连续,如果不连续重新计数, 并把当前计数比较给最大计数
                if (IsOk == "yes")
                {
                    if (PreV != i - 1)
                    {
                        if (MaxCount < iCount) { MaxCount = iCount; }
                        iCount = 0;
                        PreV = i;
                    }
                    else
                    {
                        iCount++;
                        PreV = i;
                    }
                }


            }
            if (MaxCount < iCount) { MaxCount = iCount; }
            RainCount = MaxCount;
            //二、温度查询 
            f_strsql = "  select  Max(maxtemp) As MaxTemp , min(Mintemp) as Mintemp , "
                              + "avg( (Convert( float,maxtemp) +Convert(float, mintemp )) / 2 ) as Avgtemp  "
                              + "from rffc_tab_element  where  Convert( float,maxtemp) < 50.0 and Convert( float,mintemp)  > -50.0 "
                              + " and maxtemp <>'' and mintemp <> ''  and    stationnum = '" + staid + "' "
                              + " and ObservTm >= '" + bDate + "0000' "
                              + " and ObservTm <= '" + eDate + "2300'  and  ObservTm like '%1630'   and time_hh=24";
            f_ds2 = ado1.sel1(f_strsql);
            f_dr2 = f_ds2.Tables[0].Rows[0];
            if (f_dr2["AvgTemp"].ToString() != "")
                AvgTemp = Convert.ToDouble(f_dr2["AvgTemp"]);
            else
                AvgTemp = 0;
            if (f_dr2["MaxTemp"].ToString() != "")
                MaxTemp = Convert.ToDouble(f_dr2["MaxTemp"]);
            else
                MaxTemp = 0;
            if (f_dr2["MinTemp"].ToString() != "")
                MinTemp = Convert.ToDouble(f_dr2["MinTemp"]);
            else
                MinTemp = 0;
            //三、最大风速 
            f_strsql = "  select Max(B.Code)  As MaxWv   "
                              + "from rffc_tab_element A , rffc_wind_discr B   where   A.windspeed = B.wind_class   "
                              + " and   stationnum = '" + staid + "' "
                              + " and ObservTm >= '" + bDate + "0000' "
                              + " and ObservTm <= '" + eDate + "2300'  and  ObservTm like '%1630'   and time_hh=24";
            f_ds2 = ado1.sel1(f_strsql);
            f_dr2 = f_ds2.Tables[0].Rows[0];
            if (f_dr2["MaxWv"].ToString() != "")
                WindV = Convert.ToInt32(f_dr2["MaxWv"]);
            else
                WindV = 0;

            f_strsql = "  select  wind_class  from  rffc_wind_discr  where    code = '" + WindV + "'";
            f_ds2 = ado1.sel1(f_strsql);
            f_dr2 = f_ds2.Tables[0].Rows[0];

            Wind = f_dr2["wind_class"].ToString();
        }
        #endregion

    }
}
